<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->

							<div id="Number">

							</div>
							<input type="hidden"  id="page" value="1" size="1px" >
							<input type="hidden"  id="pages" size="1px" >
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">
			<form id="add_form">
				<table class="table table-bordered table-striped"  width="800px">
					<tr>
						<td>广告分类</td>
						<td>
							<select class="form-control" name="categoryId" id="cateId">
								<option th:each="l :${list}" th:value="${l.id}">[[${l.name}]]</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>标题</td>
						<td><input  class="form-control" name="title" placeholder="标题" >  </td>
					</tr>
					<tr>
						<td>URL</td>
						<td><input  class="form-control" name="url" placeholder="URL" >  </td>
					</tr>
					<tr>
						<td>排序</td>
						<td><input  class="form-control" name="sortOrder"  placeholder="排序" >  </td>
					</tr>
					<tr>
						<td>广告图片</td>
						<td>
							<table class="table table-bordered table-striped">
								<tr>
									<td>商品图片</td>
									<td>
										<table>
											<tr>
												<td>
													<input type="file" name="itemImages" id = "file" />
													<input type = "text" name = "pic" id = "itemImages">
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>是否有效</td>
						<td>
							<input type="checkbox" value="1" name="status" class="icheckbox_square-blue" >
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="insertContents()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">广告编辑</h3>
			  </div>
			  <div class="modal-body">
				  <form id="update_form">
					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>广告分类</td>
							  <td>
								  <select class="form-control" name="categoryId" id="cateId_update">
									  <option th:each="l :${list}" th:value="${l.id}">[[${l.name}]]</option>
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>id</td>
							  <td><input  class="form-control" name="id" id="id" disabled placeholder="id" >  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input  class="form-control" name="title" id="title" placeholder="标题" >  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input  class="form-control" name="url" id="url" placeholder="URL" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input  class="form-control" name="sortOrder" id="sortOrder"  placeholder="排序" >  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table class="table table-bordered table-striped">
									  <tr>
										  <td>商品图片</td>
										  <td>
											  <table>
												  <tr>
													  <td>
														  <input type="file" name="itemImages" id = "file_updata" />
														  <input type = "text" name = "pic" id="itemImages_update">
													  </td>
												  </tr>
											  </table>
										  </td>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td>
								  <input type="checkbox" value="1" name="status" id="status" class="icheckbox_square-blue" >
							  </td>
						  </tr>
					  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" onclick="updateContents()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css">
<link rel="stylesheet" href="../plugins/select2/select2.css" />
<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
</html>
<script>
	$(function (){

		$("#cateId").select2();
		$("#cateId_update").select2();

			$("#file").fileinput({
				language : "zh",
				uploadUrl : "/file/upload",
				maxFileSize : 2048,
				maxFileCount: 3,
				allowedFileExtensions: ["jpg", "png", "gif"],
				enctype : "multipart/form-data"
			})
			$("#file").on("fileuploaded", function (event, data, previewId, index) {
				$("#itemImages").val(data.response.data)
			})

		//修改
		$("#file_updata").fileinput({
			language : "zh",
			uploadUrl : "/file/upload",
			maxFileSize : 2048,
			maxFileCount: 3,
			allowedFileExtensions: ["jpg", "png", "gif"],
			enctype : "multipart/form-data",
			nitialPreviewAsData: true,
			initialPreviewFileType: "image",
			initialPreview : '<img src = " '+ $("#itemImages_update").val() + '" />'
		})
		$("#file_updata").on("fileuploaded", function (event, data, previewId, index) {
			$("#itemImages_update").val(data.response.data)
		})

		getContentList();
	})


	function getContentList(){
		var page = parseInt($("#page").val())
		$.ajax({
			url:"content/getContentList",
			data:{pageNumber:page},
			dataType:"json",
			type:"get",
			success:function (result){
				if(result.code == 10000){
					var str='';
					var data = result.data.rows;
					console.log(result);
					for (var i = 0; i < data.length; i++) {
						str+='<tr>'
						str+='<td><input type="checkbox"></td>'
						str+='<td>'+data[i].id+'</td>'
						str+='<td>'+data[i].categoryId+'</td>'
						str+='<td>'+data[i].title+'</td>'
						str+='<td>'+data[i].url+'</td>'
						str+='<td>'
						str+='<img alt="" src="'+data[i].pic+'" width="100px" height="50px">'
						str+='</td>'
						str+='<td>'+data[i].sortOrder+'</td>'
						str+='<td>'+getStatus(data[i].status)+'</td>'
						str+='<td class="text-center">'
						str+='<button type="button" class="btn bg-olive btn-xs" onclick="getContentById('+data[i].id+')" data-toggle="modal"data-target="#updateEditModal">修改 </button>'
						str+='</td>'
						str+='</tr>'
					}
					$("#tbody").html(str);

					//分页
					var num = ''
					num += '<a href="javascript:;" onclick="toPage(3)">首页</a>';
					num += '<a href="javascript:;" onclick="toPage(1)">上一页</a>';
					num += '<a href="javascript:;" onclick="toPage(2)">下一页</a>';
					num += '<a href="javascript:;" onclick="toPage(4)">末页</a>';
					$("#Number").html(num);
					$("#pages").val(result.data.pages);
					$("#total").val(result.data.total);
					$("#pageSize").val(result.data.pageSize);
				}else{
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}

	function getStatus(obj){
		if(obj == "1"){
			return "有效";
		}else{
			return "无效";
		}
	}

	//分页
	function toPage(sb){
		var page = parseInt($("#page").val());
		var pages = parseInt($("#pages").val());
		if(sb == 1){
			$("#page").val(page - 1);
		}else if(sb == 2){
			if (page!=pages){
				$("#page").val(page + 1);
			}
		}else if(sb == 3){
			$("#page").val(1);
		}else if(sb == 4){
			$("#page").val(pages);
		}else{
			$("#page").val(page = sb);
		}
		getContentList()
	}

	function insertContents(){
		$.ajax({
			url:"content/insertContents",
			data:$("#add_form").serialize(),
			dataType:"json",
			type:"get",
			success:function (result){
				if(result.code == 10000){
					alert(result.message);
					location.reload()
				}else{
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}

	function getContentById(id){
		$.ajax({
			url:"content/getContentById",
			data:{id:id},
			dataType:"json",
			type:"get",
			success:function (result){
				console.log(result)
				if(result.code == 10000){
					$("#id").val(result.data.id)
					$("#name").val(result.data.name)
					$("#title").val(result.data.title)
					$("#itemImages_update").val(result.data.pic)
					$("#sortOrder").val(result.data.sortOrder)
					$("#url").val(result.data.url)
					$("#cateId_update").val(result.data.categoryId).trigger("change")
					var ras = $("#status")
					for (var i = 0; i < ras.length; i++) {
						if (ras[i].value == result.data.status) {
							$(ras[i]).attr("checked", "checked")
						}
					}
				}else{
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}

	function updateContents(){
		$.ajax({
			url:"content/updateContents",
			data:$("#update_form").serialize()+"&id="+$("#id").val(),
			dataType:"json",
			type:"get",
			success:function (result){
				if(result.code == 10000){
					alert(result.message);
					location.reload()
				}else{
					alert(result.message);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
</script>